package cn.itcast.bos.service.base.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.struts2.components.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.fixedActionService;
import cn.itcast.bos.web.Dao.base.CourierRepository;
import cn.itcast.bos.web.Dao.base.fixedAreaRepository;
import cn.itcast.bos.web.Dao.teke_delivary.TakeTimeRepository;
import cn.itcast.bos.web.action.base.fixedArea;

@Service
@Transactional
public class fixedAreaServiceimpl implements fixedActionService {
	// 注入Dao
	@Autowired
	private fixedAreaRepository repository;

	@Override
	public void save(FixedArea fixedArea) {
		repository.save(fixedArea);

	}

	// 分页查询
	@Override
	public Page<FixedArea> findPageData(Specification<FixedArea> specification, Pageable pageable) {

		return repository.findAll(specification, pageable);
	}

	// 查询未关蓝定区的快地缘‘
	@Override
	public List<Courier> findNoAssociation() {
		// 封装Specification --------为什么要封装>????
		Specification<Courier> specification = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				return null;
			}
		};

		return null;
	}

	// 注入 courier take
	private CourierRepository courierRepository;
	private TakeTimeRepository takeTimeRepository;

	// 关联快递员
	@Override
	public void associationCourierToFixedArea(FixedArea model, Integer courierId, Integer takeTimeId) {
		FixedArea persistFixedArea = repository.findOne(model.getId());
		Courier courier = courierRepository.findOne(courierId);
		TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
		// 将快递员关联到定区上
		persistFixedArea.getCouriers().add(courier);
		// 将收派标准 关联到快递员上
		courier.setTakeTime(takeTime);

	}

}
